<template>
  <div>
    <!-- 子组件-->
    <commonBox tow="分类管理" one="分类列表"> </commonBox>
    <!-- 卡片 -->
    <el-card>
      <el-alert
        show-icon
        title="注意，只允许第三级相关参数"
        type="warning"
        :closable="false"
      >
      </el-alert>
      <el-row>
        <el-col>
          <span>选择商品分类: </span>
          <!-- 选择框 -->
          <el-cascader
            v-model="value"
            :options="catelist"
            :props="cateprops"
            @change="handleChange"
          ></el-cascader>
        </el-col>
      </el-row>
      <!-- tab  -->
      <el-tabs v-model="activeName" @tab-click="tabhandleChange">
        <el-tab-pane label="动态参数" name="many">
          <el-button
            type="primary"
            size="mini"
            @click="addbox"
            :disabled="tabhandleClick"
            >添加参数</el-button
          >
          <!-- 动态表格 -->
          <el-table :data="manydata" stripe border>
            <el-table-column type="expand" label="#">
              <!-- 展开项？ -->
              <template slot-scope="scope">
                <el-tag
                  closable
                  @close="tabclosebox(scope.row, index)"
                  v-for="(item, index) in scope.row.attr_vals"
                  :key="index"
                >
                  {{ item }}
                </el-tag>
                <!-- 添加标签 -->
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="tabshowInput(scope.row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column prop="attr_name" label="参数名称">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" size="mini">编辑</el-button>
                <el-button type="danger" size="mini">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <el-button type="primary" size="mini" 
           @click="addbox"
          :disabled="tabhandleClick"
            >添加属性</el-button
          >
          <!-- 静态表格 -->
          <el-table :data="onlydata" stripe border>
            <el-table-column type="expand" label="#">
              <!-- 展开项 -->
             <template slot-scope="scope">
                <el-tag
                  closable
                  @close="tabclosebox(scope.row, index)"
                  v-for="(item, index) in scope.row.attr_vals"
                  :key="index"
                >
                  {{ item }}
                </el-tag>
                <!-- 添加标签 -->
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="tabshowInput(scope.row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column prop="attr_name" label="参数名称">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" size="mini">编辑</el-button>
                <el-button type="danger" size="mini">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- add增加弹框 -->
    <el-dialog
      title="增加属性"
      @close="addboxcolse"
      :visible.sync="adddialogVisible"
      width="50%"
    >
      <!-- 表单验证 -->
      <el-form
        :model="addruleForm"
        :rules="addrules"
        ref="addruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="动态参数" prop="cat_name">
          <el-input v-model="addruleForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialoadddialogVisiblegVisible = false"
          >取 消</el-button
        >
        <el-button type="primary" @click="addsub">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // add增加
      adddialogVisible: false,
      //  add增加验证
      addrules: {
        cat_name: [
          {
            required: true,
            message: "请输入活动名称",
            trigger: "blur",
          },
        ],
      },
      // add表单数据
      addruleForm: {
        cat_name: "",
      },
      // 选择器列表数据
      catelist: [],
      // 配置对象props
      value: [],
      cateprops: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
        expandTrigger: "hover",
        checkStrictly: true,
      },
      // tab
      activeName: "many",
      // 动态参数数据？
      manydata: [],
      // 静态属性
      onlydata: [],
      // 添加标签boolear
      // inputVisible: false,
      // //  添加 标签input值
      // inputValue: "",
    };
  },
  created() {
    this.getcatelist();
  },
  methods: {
    async getcatelist() {
      const { data: res } = await this.$http.get("categories");
      console.log(res);
      this.catelist = res.data;
    },
    // 级联选择器改变事件
    handleChange(value) {
      console.log(value, "...");
      if (this.value.length !== 3) {
        return (this.value = []);
      }
      this.getservedata();
    },
    // tab 切换是事件
    tabhandleChange() {
      console.log(this.activeName, ";;;");
      this.getservedata();
    },
    // 动态参数和静态属性请求数据列表
    async getservedata() {
      const { data: res } = await this.$http.get(
        `categories/${this.attr_id}/attributes`,
        {
          params: { sel: this.activeName },
        }
      );
      console.log(res, "///");
      if (res.meta.status !== 200) {
        return this.$message.error("列表失败");
      }
      this.$message.success("获取列表成功");
      res.data.forEach((item) => {
        item.attr_vals = item.attr_vals.split(",");
        item.inputVisible = false;
        item.inputValue = "";
      });
      if (this.activeName == "many") {
        this.manydata = res.data;
      } else {
        this.onlydata = res.data;
      }
    },
    // input框事件
    async handleInputConfirm(role) {
      if (role.inputValue.trim().length == 0) {
        role.inputVisible = false;
        role.inputValue = "";
        return
      }
      role.attr_vals.push(role.inputValue);
      role.inputVisible = false;
      role.inputValue = "";
      this.addclosedata(role);
    },
    // tab点击按钮事件
    tabshowInput(role) {
      role.inputVisible = true;
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    // tab关闭事件
    tabclosebox(role, i) {
      role.attr_vals.splice(i, 1);
      this.addclosedata(role);
    },
    // 增加删除参数分装
    async addclosedata(role) {
      const { data: res } = await this.$http.put(
        `categories/${this.attr_id}/attributes/${role.attr_id}`,
        {
          attr_name: role.attr_name,
          attr_sel: this.activeName,
          attr_vals: role.attr_vals.join(","),
        }
      );
      console.log(res);
    },
    // 点击添加弹框
    addbox() {
      this.adddialogVisible = true;
    },
    // 与验证
    addboxcolse() {
      this.$refs.addruleForm.resetFields();
    },
    // 确认添加参数
   async addsub() {
      this.$refs.addruleForm.validate(async (valid) => {
        if (!valid) return;
         const {data:res}=await this.$http.post(`categories/${this.attr_id}/attributes`,{
           attr_name:this.addruleForm.cat_name,
           attr_sel:this.activeName
         })
         console.log(res);
        this.getservedata();
         this.adddialogVisible=false
      });
    },
  },
  computed: {
    tabhandleClick() {
      if (this.value.length == 3) {
        return false;
      }
      return true;
    },
    // 定义分类id
    attr_id() {
      if (this.value.length !== 3) {
        return (this.value = []);
      } else {
        return this.value[this.value.length - 1];
      }
    },
  },
};
</script>
<style lang="less" scoped>
.el-row {
  margin-top: 10px;
  margin-bottom: 10px;
}
.el-tag {
  margin: 10px;
}
.input-new-tag {
  width: 200px;
}
.el-button {
  margin-bottom: 10px;
}
</style>